package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.ResultBean;
import cst.neu.yuejian_31.iface.UpPostIface;
import cst.neu.yuejian_31.iface.UpPostListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.UpPostService;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/11/2.
 */

public class UpPostModel implements UpPostIface {
    private Retrofit retrofit;
    public UpPostModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void uploadFile(int userId, String postIntro, RequestBody imgs, final UpPostListener upPostListener) {
        UpPostService service = retrofit.create(UpPostService.class);
        Call<ResultBean>call=
                service.uploadFile(userId,postIntro,imgs);
        call.enqueue(new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    upPostListener.onSuccess(response.body());
                }else {
                    upPostListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                upPostListener.onFail(t.toString());
            }
        });
    }
}
